<?php
    $a = mysqli_connect("localhost","root","gala123456");
    $db = mysqli_select_db($a,"sheep");
    $sql2 = "SELECT `avatar` FROM `user` WHERE `openid` = '".$_POST["openid"]."'";
    $query2 = mysqli_query($a,$sql2);
    $new_nei;
    while ($row2 = mysqli_fetch_row($query2)) {
        foreach ($row2 as $nei2) {
            if ($nei2 == null) {
                move_uploaded_file($_FILES["file"]["tmp_name"],"avatar/".$_FILES["file"]["name"]);
                $a = mysqli_connect("localhost","root","gala123456");
                $db = mysqli_select_db($a,"sheep");
                $sql = "UPDATE `user` SET `avatar`= 'https://726y7113a8.zicp.fun/API/avatar/".$_FILES["file"]["name"]."' WHERE `openid` = '".$_POST["openid"]."'";
                $query = mysqli_query($a,$sql);
            }else{
                $new_nei = explode("/",$nei2);
                $file = "avatar/".$new_nei[5];
                if (file_exists($file)) {
                    unlink("avatar/".$new_nei[5]);
                }
                move_uploaded_file($_FILES["file"]["tmp_name"],"avatar/".$_FILES["file"]["name"]);
                $a = mysqli_connect("localhost","root","gala123456");
                $db = mysqli_select_db($a,"sheep");
                $sql = "UPDATE `user` SET `avatar`= 'https://726y7113a8.zicp.fun/API/avatar/".$_FILES["file"]["name"]."' WHERE `openid` = '".$_POST["openid"]."'";
                $query = mysqli_query($a,$sql);
            }
        }
    }
    echo json_encode("https://726y7113a8.zicp.fun/API/avatar/".$_FILES["file"]["name"]);
?>